package com.whq.service.impl;

import com.whq.entity.ExcelBean;
import com.whq.entity.Order;
import com.whq.entity.SsUser;
import com.whq.entity.User;
import com.whq.service.CreditInfoService;
import com.whq.service.OrderService;
import com.whq.service.SsUserService;
import com.whq.utils.ExcelUtils;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.beans.IntrospectionException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by qpx on 2018/4/20.
 */
@Service
public class CreditInfoServiceImpl implements CreditInfoService {

    @Autowired
    OrderService orderService;

    @Autowired
    SsUserService ssUserService;

    @Override
    public void uploadPayerCreditInfoExcel(InputStream in, MultipartFile file) throws Exception {
        List<List<Object>> listob = ExcelUtils.getBankListByExcel(in,file.getOriginalFilename());
        List<SsUser> users = new ArrayList<SsUser>();
        for (int i = 0; i < listob.size(); i++) {
            List<Object> ob = listob.get(i);
            SsUser user = new SsUser();
            user.setLoginName(String.valueOf(ob.get(1)));
            user.setName(String.valueOf(ob.get(2)));
//            order.setOrderNum(String.valueOf(ob.get(0)));
//            order.setAddress(String.valueOf(ob.get(1)));
//            order.setCarBrand(String.valueOf(ob.get(2)));
//            BigDecimal bd=new BigDecimal(String.valueOf(ob.get(3)));
//            order.setCouponPrice(bd.setScale(2, BigDecimal.ROUND_HALF_UP));
//            order.setCityName(String.valueOf(ob.get(4)));
//            order.setRemark(String.valueOf(ob.get(5)));
//            orders.add(order);
        }
        ssUserService.insertBatch(users);
    }

    public XSSFWorkbook exportPayerCreditInfoExcel() throws Exception{


        SsUser user = new SsUser();
        String beginTime = "2016-11-01 00:00:00";
        String endTime = "2016-11-30 23:59:59";
        user.setBeginTime(beginTime);
        user.setEndTime(endTime);
        List<SsUser> orders = ssUserService.selectByCondition(user);

        List<ExcelBean> ems=new ArrayList<ExcelBean>();
        Map<Integer,List<ExcelBean>> map=new LinkedHashMap<Integer,List<ExcelBean>>();
        XSSFWorkbook book=null;
        ems.add(new ExcelBean("id","id",0));
        ems.add(new ExcelBean("登录名称","loginName",0));
        ems.add(new ExcelBean("真实姓名","name",0));
//		ems.add(new ExcelBean("买方是否参与","isBuyerIquidation",0));
        ems.add(new ExcelBean("密码","password",0));
        ems.add(new ExcelBean("密码提示","salt",0));
        ems.add(new ExcelBean("创建时间","createtime",0));
        ems.add(new ExcelBean("邮箱","email",0));
        ems.add(new ExcelBean("状态","status",0));
        ems.add(new ExcelBean("电话","mobile",0));
        map.put(0, ems);
        book=ExcelUtils.createExcelFile(SsUser.class, orders, map, "用户登录信息");
        return book;
    }

}
